package models

import (
	"github.com/go-xorm/xorm"
)

// 主机
type DHost struct {
	Id           int16  `json:"id" xorm:"smallint pk autoincr"`
	Name         string `json:"name" xorm:"varchar(64) notnull"`                // 主机别名
	Host         string `json:"host" xorm:"varchar(32) notnull default '' "`    // 数据库服务器名称
	Port         int64  `json:"port" xorm:"notnull default 22"`                 // 主机端口
	Remark       string `json:"remark" xorm:"varchar(100) notnull default '' "` // 备注
	Databasetype string `json:"databasetype" xorm:"varchar(64) notnull"`
	User         string `json:"user" xorm:"varchar(64) notnull"`
	Password     string `json:"password" xorm:"varchar(100) notnull"`
	Database     string `json:"database" database:"varchar(64) notnull"`
	Charset      string `json:"charset" xorm:"varchar(32) notnull default '' "`
	Locate       string `json:"locate" xorm:"varchar(256) notnull default '' "` // 备份位置
	Version      string `json:"version" xorm:"varchar(32) notnull default '' "`
	Sqlpath      string `json:"sqlpath" xorm:"varchar(256) notnull default '' "`
	Local        int16  `json:"local" xorm:"smallint notnull default 0 "`
	BaseModel    `json:"-" xorm:"-"`
	Selected     bool `json:"-" xorm:"-"`
}

type DCurHost struct {
	Id           int16  `json:"id" `
	Name         string `json:"name" `   // 主机别名
	Host         string `json:"host" `   // 数据库服务器名称
	Port         int64  `json:"port" `   // 主机端口
	Remark       string `json:"remark" ` // 备注
	Databasetype string `json:"databasetype"`
	User         string `json:"user"`
	Password     string `json:"password" `
	Database     string `json:"database" `
	Charset      string `json:"charset" `
	Locate       string `json:"locate" ` // 备份位置
	Version      string `json:"version" `
	Sqlpath      string `json:"sqlpath" `
}

// 返回表名
func (task *DCurHost) TableName() string {
	return TablePrefix + "d_host" //TablePrefix +
}

// 新增
func (dhost *DHost) Create() (insertId int16, err error) {
	_, err = Db.Insert(dhost)
	if err == nil {
		insertId = dhost.Id
	}

	return
}

func (dhost *DHost) UpdateBean(id int16) (int64, error) {
	if dhost.Password == "" {
		return Db.ID(id).Cols("host,name,alias,port,remark,databasetype,user,database,locate,version,sqlpath").Update(dhost)

	} else {
		return Db.ID(id).Cols("host,name,alias,port,remark,databasetype,user,database,locate,version,password,sqlpath").Update(dhost)
	}
}

// 更新
func (dhost *DHost) Update(id int, data CommonMap) (int64, error) {
	return Db.Table(dhost).ID(id).Update(data)
}

// 删除
func (dhost *DHost) Delete(id int) (int64, error) {
	return Db.Id(id).Delete(new(Host))
}

func (dhost *DHost) Find(id int) error {
	_, err := Db.Id(id).Get(dhost)

	return err
}

func (dhost *DHost) GetDHostIdsById(hostId int) ([]DCurHost, error) {
	list := make([]DCurHost, 0)
	//fields := "th.id,th.host_id,h.alias,h.name,h.port" //,th.task_id
	err := Db. //Join("LEFT", hostTableName(), "th.host_id=h.id").
			Where("id = ?", hostId).
		//Cols(fields).
		Find(&list)

	return list, err
}

func (dhost *DHost) NameExists(name string, id int16) (bool, error) {
	if id == 0 {
		count, err := Db.Where("name = ?", name).Count(dhost)
		return count > 0, err
	}

	count, err := Db.Where("name = ? AND id != ?", name, id).Count(dhost)
	return count > 0, err
}

func (dhost *DHost) List(params CommonMap) ([]DHost, error) {
	dhost.parsePageAndPageSize(params)
	list := make([]DHost, 0)
	session := Db.Desc("id")
	dhost.parseWhere(session, params)
	err := session.Limit(dhost.PageSize, dhost.pageLimitOffset()).Find(&list)

	return list, err
}

func (dhost *DHost) AllList() ([]DHost, error) {
	list := make([]DHost, 0)
	err := Db.Cols("name,port").Desc("id").Find(&list)

	return list, err
}

func (dhost *DHost) Total(params CommonMap) (int64, error) {
	session := Db.NewSession()
	dhost.parseWhere(session, params)
	return session.Count(dhost)
}

// 解析where
func (dhost *DHost) parseWhere(session *xorm.Session, params CommonMap) {
	if len(params) == 0 {
		return
	}
	id, ok := params["Id"]
	if ok && id.(int) > 0 {
		session.And("id = ?", id)
	}
	name, ok := params["Name"]
	if ok && name.(string) != "" {
		session.And("name = ?", name)
	}
}
